<template>
  <div>
    <SelectCannibalizeInputModal ref="inputModal" :billStatus="billStatus" :urlList="this.url.urlList" :multi="false" @choseObj="choseObj"></SelectCannibalizeInputModal>
  </div>
</template>

<script>
  import SelectCannibalizeInputModal from './modules/SelectCannibalizeInputModal'
  import { getAction } from '@/api/manage'

  export default {
    name: 'SelectCannibalizeInput',
    components: { SelectCannibalizeInputModal },
    props: {
      isHandleShopping: {
        type: Boolean,
        default: false
      },
      value: {
        type: Array,
        default: () => []
      },
      billStatus: {
        type: String,
        default: '',
        required: false
      },
    },
    data() {
      return {
        ids: [],
        selectedObj: [],
        deleteIds: [],
        list: [],
        objs: '',
        url: {
          queryById: '/api/erp/purchase/procurement/erpPurchaseCalibration/queryById',
          queryByIds: '/api/erp/purchase/procurement/erpPurchaseCalibration/queryByIds',
          urlList: '/api/erp/purchase/procurement/erpPurchaseCalibration/allList'
        }
      }
    },
    mounted() {
      // debugger;
      // this.initObj(this.value);
    },
    watch: {
      isHandleShopping: {
        handler (val) {
          if (val) {
            this.initObj(this.value)
          }
        },
        immediate: true
      }
    },
    model: {
      prop: 'value',
      event: 'change'
    },
    methods: {
      initObj(ids) {
        console.log(ids);
        // this.deleteIds = [];
        // this.selectedObj = [];
        if (ids && ids.length > 0) {
          this.ids = ids.split(',');
          let url = '';
          let param = {};
          if (this.ids.length == 1) {
            url = this.url.queryById;
            param = { id: ids };
          } else {
            url = this.url.queryByIds;
            param = { ids: ids };
          }
          getAction(url, param).then((res) => {
            if (res.success) {
              if (!Array.isArray(res.result)) {
                this.selectedObj.push(res.result);
              } else {
                for (let i = 0; i < res.result.length; i++) {
                  this.selectedObj.push(res.result[i]);
                }
              }
              this.$refs.inputModal.edit(res.result, this.ids);
            }
          });
        } else {
          this.ids = [];
          this.$refs.inputModal.edit(null, null);
        }
        this.selectIds()
      },
      // resetObj() {
      //   this.userType = false;
      //   this.ids = [];
      //   this.selectedObj = [];
      //   this.deleteIds = [];
      //   this.open = false;
      //   this.$refs.inputModal.edit(null, null);
      // },
      selectIds() {
        if (!this.disabled) {
          this.deleteId();
          this.$refs.inputModal.add(this.selectedObj, this.ids);
        }
      },
      // deselect(value, option) {
      //   this.deleteIds.push(value);
      //   this.deleteId();
      //   this.choseObj(this.selectedObj);
      // },
      deleteId() {
        let that = this;
        this.selectedObj = this.selectedObj.filter(function (e) {
          return that.deleteIds.indexOf(e.id) == -1;
        })
        this.deleteIds = [];
      },
      // 子modal回调
      choseObj: function (list) {
        this.selectedObj = list;
        this.ids = [];
        for (var i = 0; i < list.length; i++) {
          this.ids.push(list[i].id);
        }
        this.objs = this.ids.join(',');
        this.$emit('change', this.objs);
        this.$emit('back', this.selectedObj);
      }
    }
  }
</script>

<style scoped>

</style>
